<!--
盲审抽取比例设置新增
-->
<template>
    <div>
        <div>
            <el-form :model="form" ref="form" label-suffix="：" :rules="rules" label-width="100px">
                <el-row :gutter="10">
                    <el-col :span="12">
                        <el-form-item :label="$t('label.college')" prop="academyCode">
                          <el-select
                            v-model="form.academyCode"
                            style="width: 100%;"
                            :clearable="false"
                            filterable
                            :placeholder="$t('common.text.all')"
                            v-clearreadonly
                          >
                            <el-option
                                v-for="item in options.academyCode"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            />
                          </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item :label="$t('paperBlindTrial.batchName')" prop="batchNo">
                          <el-input v-model="form.batchNo" :placeholder="$t('common.pla.pleaseInput')" :maxlength="255"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                      <el-form-item :label="$t('paperBlindTrial.tiemName')" prop="defenseDate">
                        <el-date-picker
                          :clearable="true"
                          v-model="form.defenseDate"
                          type="datetimerange"
                          range-separator="~"
                          :start-placeholder="$t('message.startTime')"
                          :end-placeholder="$t('message.endTime')"
                          format="yyyy-MM-dd HH:mm:ss"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          :default-time="['00:00:00', '23:59:59']"
                          style="width:100%;">
                        </el-date-picker>
                      </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </div>
    </div>
</template>

<script>
import { timeSettingAdd, timeSettingGetDetail } from "common/src/api/paperBlindTrial";
import commonMixins from 'common/src/views/practiceManagement/commonMixins';
import mixin from 'common/src/mixins/index';
export default {
    components: {},
    mixins: [commonMixins,mixin],
    props: {
      objs: {
        type: Object,
      },
      options: {
        type: Object,
      }
    },
    data() {
        return {
            form: {
              batchNo: '', // 批次
              academyCode: '', // 学院
              dateStart: '', // 开始时间
              dateEnd: '', // 结束时间
              defenseDate: [] // 时间范围值
            },
            rules: {
              // 管理员账号学院不是必填，教务员账号学院必填
              academyCode: [],
              defenseDate: [{ required: true, message: this.$t('common.pla.pleaseSelect'), trigger: 'change' }],
              batchNo: [{ validator: this.validateRemark, trigger: 'blur' }],
            },
        };
    },
    computed: {

    },
    mounted() {
      if (this.objs.id) {
        this.getDetail();
      }
      // 教务员设置时间时，学院必填
      if (this.global.isAcdemicDean) {
        this.rules.academyCode = [{ required: true, message: this.$t('common.pla.pleaseSelect'), trigger: 'change' }];
      } else {
        this.rules.academyCode = [];
      }
    },
    methods: {
      save(flag) { // flag = edit 修改
        this.$refs.form.validate((valid) => {
          if (valid) {
            let param = {
              "id": this.objs.id,//修改时传入
              "academyCode": this.form.academyCode,
              "batchNo": this.form.batchNo,
              "dateStart": this.form.defenseDate[0],
              "dateEnd": this.form.defenseDate[1]
            };
            timeSettingAdd(param).then(res => {
              if (res.code === 200) {
                if (flag === 'edit') {
                  this.$message.success(this.$t('common.tips.editSuccess'));
                } else {
                  this.$message.success(this.$t('paperBlindTrial.submitSuccess'));
                }
                this.$emit('emit:back');
              }
            });
          }
        });
      },
      // 根据id获取规则详情
      getDetail () {
        timeSettingGetDetail(this.objs.id).then(res => {
          if (res.code === 200) {
            let resultData = res.data;
            this.form = {
              batchNo: resultData.batchNo, // 批次
              academyCode: resultData.academyCode, // 学院
              defenseDate: [resultData.dateStart, resultData.dateEnd] // 时间范围值
            };
          }
        });
      },
      // 校验批次，只能输入数字，最大不超过100
      validateBatchNo (rule, value, callback) {
        let intReg = /^[1-9][0-9]*$/; // 正整数
        if (!value || value === '') {
          return callback(new Error(this.$t('common.pla.pleaseInput')));
        }
        if (!intReg.test(value)) {
          return callback(new Error(this.$t('paperBlindTrial.moreThenZero')));
        }
        if (parseInt(value) > 100) {
          return callback(new Error(this.$t('paperBlindTrial.lessThen')));
        }
        return callback();
      }
    }
};
</script>

<style scoped lang="scss">
.color {
    &_no {
        color: #ccc;
        margin-right: 5px;
    }
    &_yes {
        color: #222;
        margin-left: 5px;
    }
}
</style>
